package org.mall.modules.pay.controller.admins;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.mall.common.api.vo.CommonResult;
import org.mall.common.api.vo.Result;
import org.mall.modules.pay.service.PayTransactionService;
import org.mall.modules.pojo.pay.transaction.PayTransactionBO;
import org.mall.modules.pojo.pay.transaction.PayTransactionPageBO;
import org.mall.modules.pojo.pay.transaction.PayTransactionPageDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import static org.mall.common.api.vo.CommonResult.success;

@RestController
@RequestMapping("/admins/transaction")
public class AdminsPayTransactionController {

   @Autowired
    private PayTransactionService payTransactionService;

    @GetMapping("/page")
    public Result<?> page(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
                                                   @RequestParam(value = "createBeginTime", required = false) Date createBeginTime,
                          @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
                                                   @RequestParam(value = "createEndTime", required = false) Date createEndTime,
                          @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
                                                   @RequestParam(value = "paymentBeginTime", required = false) Date paymentBeginTime,
                          @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
                                                   @RequestParam(value = "paymentEndTime", required = false) Date paymentEndTime,
                          @RequestParam(value = "status", required = false) Integer status,
                          @RequestParam(value = "id", required = false) Integer id,
                          @RequestParam(value = "hasRefund", required = false) Boolean hasRefund,
                          @RequestParam(value = "payChannel", required = false) Integer payChannel,
                          @RequestParam(value = "orderSubject", required = false) String orderSubject,
                          @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PayTransactionPageDTO payTransactionPageDTO = new PayTransactionPageDTO()
                .setCreateBeginTime(createBeginTime).setCreateEndTime(createEndTime)
                .setPaymentBeginTime(paymentBeginTime).setPaymentEndTime(paymentEndTime)
                .setStatus(status).setHasRefund(hasRefund)
                .setPayChannel(payChannel).setOrderSubject(orderSubject)
                .setPageNo(pageNo).setPageSize(pageSize);
        Page<PayTransactionBO> page = new Page<>();
        // 执行查询
        PayTransactionPageBO payBo = payTransactionService.getTransactionPage(payTransactionPageDTO);
        List<PayTransactionBO> list = payBo.getList();
        page.setTotal(payBo.getTotal());
        page.setRecords(list);
        return Result.ok(page);
    }

    /**
     *  获取交易详情
     * @param id
     * @return
     */
    @GetMapping("/get")
    public CommonResult<List<PayTransactionBO>> getPayTransactionDetails(@RequestParam("id") Integer id){
        return success(payTransactionService.getTransactionList(Collections.singleton(id)));
    }
}
